package com.dyson.rabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author:Dyson
 * @time:2019/10/21 18:34
 */
public class Sender {
    public static void main(String[] args) throws IOException, TimeoutException {
        //创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.237.128");
        factory.setPort(5672);//默认端口
        factory.setUsername("root");//默认guest
        factory.setPassword("root");//默认guest
        factory.setVirtualHost("/");//默认虚拟机virtualHost
        //创建连接
        Connection connection = factory.newConnection();
        //建立通道
        Channel channel = connection.createChannel();
        //声明队列。参数: 队列名称,是否持久化,是否仅当前连接可用,是否自动删除,队列其他属性(构造参数)
        channel.queueDeclare("msgTest", true, false, false, null);
        String msg = "这是测试消息";
        //发布消息。参数：交换机,路由key,消息其他属性(路由表头等),消息字节数组
        //队列名称与路由相同，消息会路由到这个队列
        channel.basicPublish("exchangeTest", "msgTest", null, msg.getBytes("UTF-8"));
        System.out.println("发送消息:" + msg);
    }
}
